#!/bin/bash

TLNET_MIRROR=ftp.ctex.org/mirrors/texlive/tlnet

has_sub_command () {
    test "$(type -t "cmd_$1")" = 'function'
}

cmd_help () {
    echo "Available commands:"
    declare -F | sort | while read a b f; do
        [ ${f:0:4} = "cmd_" ] && echo "  " ${f:4}
    done
}

cmd_list_debs () {
    aptitude search -F "%p %V#" "~mDebian~ TeX"
}

cmd_rsync_tlpkg () {
    [ -e tlpkg ] || mkdir tlpkg
    rsync -rvz --delete --exclude=2008 --exclude=mactex* \
        rsync://$TLNET_MIRROR/tlpkg/{TeXLive,texlive.tlpdb,texlive.tlpdb.md5} \
        tlpkg
    cd tlpkg && md5sum -c texlive.tlpdb.md5
}

cmd_cmp_debs () {
    aptitude search -F "%p %V" --disable-columns "~mDebian~ TeX" | perl -Itlpkg -e '
        use TeXLive::TLPDB;
        use strict;
        use warnings;

        my $tlpdb = new TeXLive::TLPDB;
        $tlpdb->from_file("tlpkg/texlive.tlpdb");

        my ($debname, $debver, $pkgname);
        while (<>) {
            chomp;
            ($debname, $debver) = split;

            $pkgname = $debname;
            print_if_found($debname, $debver, $pkgname) && next;

            $pkgname =~ s/^texlive-doc/texlive/;
            print_if_found($debname, $debver, $pkgname) && next;

            $pkgname =~ s/^texlive-//;
            print_if_found($debname, $debver, $pkgname) && next;

            if ($pkgname =~ s/^lang-/lang/) {
                print_if_found($debname, $debver, "collection-" . $pkgname) && next;
                $pkgname =~ s/^lang/lang-/;
            }

            print_if_found($debname, $debver, "scheme-" . $pkgname) && next;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            $pkgname =~ s/-nonfree//;
            print_if_found($debname, $debver, $pkgname) && next;

            $pkgname =~ s/-doc$//;
            print_if_found($debname, $debver, $pkgname) && next;

            print_if_found($debname, $debver, "scheme-" . $pkgname) && next;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            $pkgname =~ s/-recommended$/recommended/;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            $pkgname =~ s/-extra$/extra/;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            $pkgname =~ s/-utils$/utils/;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            # for texlive-latex-base
            $pkgname =~ s/-base$//;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            $pkgname =~ s/lang-arab$/langarabic/;
            print_if_found($debname, $debver, "collection-" . $pkgname) && next;

            printf "%-30s %-30s %-30s %-30s\n", $debname, $debver, "-", "-";
        }

        sub print_if_found {
            my $tlpobj = $tlpdb->get_package($_[2]);
            if (defined $tlpobj) {
                printf "%-30s %-30s %-30s %-30s\n", $_[0], $_[1], $_[2], $tlpobj->revision;
                return 1;
            }

            return 0;
        }
    '
}
######################## main entry ####################################
cmd="${1//-}"
shift

if [ "$cmd" ] && has_sub_command "$cmd"; then
    # execute the sub command
    cmd_$cmd "$@"
else
    echo "Unknown command, run '$0 help' for help information." >&2
    exit 1
fi

